<?php

declare (strict_types=1);

namespace app\shop\controller\goods;

use cores\exception\BaseException;
use think\response\Json;
use app\shop\controller\Controller;
use app\shop\model\goods\ServiceShop as ServiceShopModel;
use app\shop\model\goods\Service as ServiceModel;
/**
 * 商户管理
 * Class Store
 * @package app\admin\controller
 */
class ServiceShop extends Controller
{
    /**
     * 获取列表记录
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function list(): Json
    {
        $model = new \app\store\model\goods\Service;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    public function getList(){
        $serviceShopModel = new ServiceShopModel();
        $serviceShopIds = $serviceShopModel->where('shop_id',$this->shop['shop_id'])->column('service_id');
        $serviceModel = new ServiceModel();
        $serviceList = $serviceModel->where('is_delete','=',0)
            ->where('store_id',$this->shop['store_id'])
            ->where('service_id','not in',$serviceShopIds)
            ->select();
        $serviceShopModel->startTrans();
        foreach ($serviceList as $k => $v){
            $data = [
                'service_id' => $v->service_id,
                'createtime' => time(),
                'store_id' => $this->shop['store_id'],
                'shop_id' => $this->shop['shop_id']
            ];
            $serviceShopModel->insert($data);
        }
        $serviceShopModel->commit();
        $list = $serviceShopModel->where('shop_id',$this->shop['shop_id'])->select();
        return $this->renderSuccess(compact('list'));
    }
}